#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2023/4/13 12:09
# @Author  : 胡浩浩
# @Email   : hu_haohao@venusgroup.com.cn
# @File    : comms.py
# @IDE     : PyCharm
# @description :
import os
import re
import uuid
import socket
from pywebio.output import close_popup, toast
from pywebio.session import eval_js

hostname = socket.gethostname()
IPADDRESS = socket.gethostbyname(hostname)


def generate_random_string(length):
    """
    获取指定长度的随机字符串
    """
    import random
    import string
    # Define the possible characters to choose from
    letters = string.ascii_lowercase
    # Use random.choices to select `length` number of characters from `letters`
    result_str = ''.join(random.choices(letters, k=length))
    return result_str


def generate_uuid():
    return str(uuid.uuid4())


def close_pop():
    close_popup()
    eval_js("window.location.reload();")


def error_toast(data):
    toast(str(data), color="red")
    return data


def check_actually_data(data):
    check_result = None
    if not data:
        error_data = error_toast("请输入数据再进行操作")
        check_result = error_data
    return check_result


def get_all_files(path):
    files = os.listdir(path)
    result = []
    for file in files:
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            result.extend(get_all_files(file_path))
        else:
            result.append(file_path)
    return result


def replace_substring_in_list(lst, pattern, replacement):
    replaced_list = []
    for item in lst:
        replaced_item = re.sub(pattern, replacement, item)
        replaced_list.append(replaced_item)
    return replaced_list
